﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
using UnityEngine.EventSystems;
public class ToPlayVideo : MonoBehaviour
{
   // public VideoClip videoClip; //视频的文件 参数
    public Text videoTimeText;  //视频时间 Text
    public Slider videoTimeSlider; //视频时间 Slider

    //定义参数获取ViderPlayer组件和RawImage组件
    internal VideoPlayer videoPlayer;
    private RawImage rawImage;
    public Text urlText;
    public string url;
    //当前视频的总时间值和当前播放时间值的参数
    private int clipHour;
    private int clipMinute;
    private int clipSecond;
    private int currentHour;
    private int currentMinute;
    private int currentSecond;
    //播放，停止按钮
    public Button btnPlayOrPause;
    public Button btnStop;
    public Text text_PlayOrPause;

    private int btnTextFlag = -1;
    // Use this for initialization
    void Start()
    {
        //获取场景中对应的组件
        videoPlayer = this.GetComponent<VideoPlayer>();
        rawImage = this.GetComponent<RawImage>();
        urlText = GameObject.Find("urlText").GetComponent<Text>();
        url = Application.streamingAssetsPath + "/"+urlText.text;
        videoPlayer.url = url;
        clipHour = (int)videoPlayer.clip.length / 3600;
        clipMinute = (int)(videoPlayer.clip.length - clipHour * 3600) / 60;
        clipSecond = (int)(videoPlayer.clip.length - clipHour * 3600 - clipMinute * 60);

        btnPlayOrPause.onClick.AddListener(PlayOrPauseVideo);
        btnStop.onClick.AddListener(StopPlay);
    }

    // Update is called once per frame
    void Update()
    {
        //如果videoPlayer没有对应的视频texture,则返回
        if (videoPlayer.texture == null)
        {
            return;
        }
        rawImage.texture = videoPlayer.texture;
        ShowVideoTime();

        //判断是否播放完成，播放暂停按钮显示播放
        if ((ulong)videoPlayer.frame == videoPlayer.frameCount && btnTextFlag != 1)
        {
            btnPlayOrPause.GetComponentInChildren<Text>().text = "播放";
            btnTextFlag = 1;
        }

        //开始播放，播放暂停按钮显示暂停
        if (videoPlayer.isPlaying == true && btnTextFlag != 0)
        {
            btnPlayOrPause.GetComponentInChildren<Text>().text = "暂停";
            btnTextFlag = 0;
        }
    }

    //显示当前视频播放时间
    private void ShowVideoTime()
    {
        //当前视频播放时间
        currentHour = (int)videoPlayer.time / 3600;
        currentMinute = (int)(videoPlayer.time - currentHour * 3600) / 60;
        currentSecond = (int)videoPlayer.time - currentHour * 3600 - currentMinute * 60;
        //把当前播放时间显示在Text上
        videoTimeText.text = string.Format("{0:D2}:{1:D2}:{2:D2}/{3:d2}:{4:D2}:{5:D2}",
            currentHour, currentMinute, currentSecond, clipHour, clipMinute, clipSecond);
        videoTimeSlider.value = (float)(videoPlayer.time / videoPlayer.clip.length);
    }

    //当前Slider比例值转换为当前的视频播放时间
    public void SetVideoTimeValueChange()
    {
        videoPlayer.time = videoTimeSlider.value * videoPlayer.clip.length;
    }

    //播放视频
    public void PlayOrPauseVideo()
    {
        if (videoPlayer.isPlaying == true)
        {
            videoPlayer.Pause();
            btnPlayOrPause.GetComponentInChildren<Text>().text = "播放";
        }
        else
        {
            videoPlayer.Play();
            btnPlayOrPause.GetComponentInChildren<Text>().text = "暂停";
        }

    }

    //停止播放
    public void StopPlay()
    {
        videoPlayer.frame = 1;
        videoPlayer.Pause();
        btnPlayOrPause.GetComponentInChildren<Text>().text = "播放";
    }

}
